<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/qtestlib.qdoc -->
<head>
  <title>Qt 4.3: Chapter 1: Writing a Unit Test</title>
  <link rel="contents" href="qtestlib-tutorial.html" />
  <link rel="next" href="qtestlib-tutorial2.html" />
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><p>
[<a href="qtestlib-tutorial.html">Contents</a>]
[Next: <a href="qtestlib-tutorial2.html">Chapter 2</a>]
</p>
<h1 align="center">Chapter 1: Writing a Unit Test<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="qtestlib-tutorial1-testqstring-cpp.html">qtestlib/tutorial1/testqstring.cpp</a></li>
</ul>
<p>In this first chapter we will see how to write a simple unit test for a class, and how to execute it.</p>
<a name="writing-a-test"></a>
<h2>Writing a Test</h2>
<p>Let's assume you want to test the behavior of our <a href="qstring.html">QString</a> class. First, you need a class that contains your test functions. This class has to inherit from <a href="qobject.html">QObject</a>:</p>
<pre> #include &lt;QtTest/QtTest&gt;

 class TestQString: public QObject
 {
     Q_OBJECT
 private slots:
     void toUpper();
 };</pre>
<p>Note that you need to include the <a href="qtest.html">QTest</a> header, and that the test functions have to be declared as private slots so the test framework finds and executes it.</p>
<p>Then you need to implement the test function itself. The implementation could look like this:</p>
<pre> void TestQString::toUpper()
 {
     QString str = &quot;Hello&quot;;
     QVERIFY(str.toUpper() == &quot;HELLO&quot;);
 }</pre>
<p>The <a href="qtest.html#QVERIFY">QVERIFY</a>() macro evaluates the expression passed as its argument. If the expression evaluates to true, the execution of the test function continues. Otherwise, a message describing the failure is appended to the test log, and the test function stops executing.</p>
<p>But if you want a more verbose output to the test log, you should use the <a href="qtest.html#QCOMPARE">QCOMPARE</a>() macro instead:</p>
<pre> void TestQString::toUpper()
 {
     QString str = &quot;Hello&quot;;
     QCOMPARE(str.toUpper(), QString(&quot;HELLO&quot;));
 }</pre>
<p>If the strings are not equal, the contents of both strings is appended to the test log, making it immediately visible why the comparison failed.</p>
<p>Finally, to make our test case a stand-alone executable, the following two lines are needed:</p>
<pre> QTEST_MAIN(TestQString)
 #include &quot;testqstring.moc&quot;</pre>
<p>The <a href="qtest.html#QTEST_MAIN">QTEST_MAIN</a>() macro expands to a simple <tt>main()</tt> method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a <tt>.cpp</tt> file, we also need to include the generated moc file to make Qt's introspection work.</p>
<a name="executing-a-test"></a>
<h2>Executing a Test</h2>
<p>Now that we finished writing our test, we want to execute it. Assuming that our test was saved as <tt>testqstring.cpp</tt> in an empty directory: we build the test using qmake to create a project and generate a makefile.</p>
<pre> /myTestDirectory$ qmake -project &quot;CONFIG += qtestlib&quot;
 /myTestDirectory$ qmake
 /myTestDirectory$ make</pre>
<p><b>Note:</b>If you're using windows, replace <tt>make</tt> with <tt>nmake</tt> or whatever build tool you use.</p>
<p>Running the resulting executable should give you the following output:</p>
<pre> ********* Start testing of TestQString *********
 Config: Using QTest library 4.1.0, Qt 4.1.0
 PASS   : TestQString::initTestCase()
 PASS   : TestQString::toUpper()
 PASS   : TestQString::cleanupTestCase()
 Totals: 3 passed, 0 failed, 0 skipped
 ********* Finished testing of TestQString *********</pre>
<p>Congratulations! You just wrote and executed your first unit test using the <a href="qtestlib-manual.html#qtestlib">QTestLib</a> framework.</p>
<p>
[<a href="qtestlib-tutorial.html">Contents</a>]
[Next: <a href="qtestlib-tutorial2.html">Chapter 2</a>]
</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
